import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path:'/index',
    name:'index',
    component:()=> import('../views/Index.vue'),
    children:[
      {
        path:'/index/home',
        name:'home',
        component:()=>import('../views/second/Home.vue')
      },
      {
        path:'/index/mine',
        name:'mine',
        component:()=>import('../views/second/Mine.vue')
      },
      {
        path:'/index/order',
        name:'order',
        component:()=>import('../views/second/Order.vue')
      },
      {
        path:'/index/tuan',
        name:'tuan',
        component:()=>import('../views/second/Tuan.vue')
      },
      {
        path:'/index',
        redirect:'/index/home'
      },
      {
        path:'*',
        component:()=>import('../views/NotFound.vue')
      }
    ]
  },
  {
    path:'/login',
    name:'login',
    component:()=>import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/detail/:id',
    name: 'detail',
    component: () => import('../views/Detail.vue')
  },
  {
    path: '/collect',
    name: 'collect',
    component: () => import('../views/Collect.vue')
  },
  {
    path:'/',
    redirect:'/index'
  },
  {
    path:'*',
    component: () => import('../views/NotFound.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  var token = localStorage.getItem('token');
  if(to.path=='/index/order'||to.path=='/index/mine'){
    if(token){
      next();
    }else{
      next('/login');
    }
  }
  next();
})
export default router
